﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.Xml;
using System.IO;
using System.Reflection;

namespace Srv3CodeTool
{
    class Config
    {
        // path
        private string szProjectPath;    // 整个工程的路径
        private string szModulePath;     // Module 这个目录的路径
        private string szModuleResPath;  // ModuleRes 这个目录的路径
        private string szModePath;       // 模板脚本的路径
        private string szPtrCheckPath;   // 检查指针路径

        // username 
        private string szUserName;      // 代码注释的时候的用户标识

        // setting
        private bool bAutoAddConsumer;    // 找不到Consumer是否自动添加
        private bool bAutoAddHeadfile;    // 转接口的时候自动添加头文件
        private bool bCommentNewCode;     // 注释新生成的代码
        private bool bPtrCheckSub;        // 指针检查的时候检查子目录

        #region 私有变量相应的属性
        public string ProjectPath
        {
            get
            {
                return szProjectPath;
            }
            set
            {
                szProjectPath = value;
            }
        }
        public string ModulePath
        {
            get
            {
                return szModulePath;
            }
            set
            {
                szModulePath = value;
            }
        }
        public string ModuleResPath
        {
            get
            {
                return szModuleResPath;
            }
            set
            {
                szModuleResPath = value;
            }
        }
        public string ModePath
        {
            get
            {
                return szModePath;
            }
            set
            {
                szModePath = value;
            }
        }
        public string UserName
        {
            get
            {
                return szUserName;
            }
            set
            {
                szUserName = value;
            }
        }
        public string PtrCheckPath
        {
            get
            {
                return szPtrCheckPath;
            }
            set
            {
                szPtrCheckPath = value;
            }
        }
        public bool AutoAddConsumer
        {
            get
            {
                return bAutoAddConsumer;
            }
            set
            {
                    bAutoAddConsumer = value;
            }
        }
        public bool AutoAddHeadfile
        {
            get
            {
                return bAutoAddHeadfile;
            }
            set
            {
                    bAutoAddHeadfile = value;
            }
        }
        public bool CommentNewCode
        {
            get
            {
                return bCommentNewCode;
            }
            set
            {
                    bCommentNewCode = value;
            }
        }
        public bool PtrCheckSub
        {
            get
            {
                return bPtrCheckSub;
            }
            set
            {
                bPtrCheckSub = value;
            }
        }
        #endregion
        /// <summary>
        /// 从XML读取信息
        /// </summary>
        public void LoadFromXmlFile()
        {
            XmlTextReader reader;
            if (File.Exists("Settings.xml"))
            {
                //优先读取外部Settings.xml文件并把信息写入blockInfo内
                reader = new XmlTextReader("Settings.xml");
            }
            else
            {
                //如果Settings.xml文件不存在,则从嵌入资源内读取Settings.xml
                Assembly asm = Assembly.GetExecutingAssembly();
                Stream sm = asm.GetManifestResourceStream("FrmOptions.Settings.xml");
                reader = new XmlTextReader(sm);
            }

            try
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name == "ProjectPath")
                        {
                            szProjectPath = reader.ReadElementString().Trim();
                        }
                        else if (reader.Name == "ModulePath")
                        {
                            szModulePath = reader.ReadElementString().Trim();
                        }
                        else if (reader.Name == "ModuleResPath")
                        {
                            szModuleResPath = reader.ReadElementString().Trim();
                        }
                        else if (reader.Name == "ModePath")
                        {
                            szModePath = reader.ReadElementString().Trim();
                        }
                        else if (reader.Name == "UserName")
                        {
                            szUserName = reader.ReadElementString().Trim();
                        }
                        else if (reader.Name == "PtrCheckPath")
                        {
                            szPtrCheckPath = reader.ReadElementString().Trim();
                        }
                        else if (reader.Name == "CommentNewCode")
                        {
                            bCommentNewCode = Convert.ToBoolean(reader.ReadElementString().Trim());
                        }
                        else if (reader.Name == "PtrCheckSub")
                        {
                            bPtrCheckSub = Convert.ToBoolean(reader.ReadElementString().Trim());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }


        /// <summary>
        /// 把信息保存为XML文件
        /// </summary>
        public void SaveToXmlFile()
        {
            XmlDocument doc = new XmlDocument();//操作XML要先声明
            doc.LoadXml("<Setting></Setting>");//把指定的文件"<BlockSet></BlockSet>"加载到XML文档内
            XmlNode root = doc.SelectSingleNode("Setting");//找到"BlockSet"这个根节点

            XmlElement xelPath = doc.CreateElement("PathSetting");
            XmlElement xelProjectPath = doc.CreateElement("ProjectPath");
            xelProjectPath.InnerText = Convert.ToString(szProjectPath);
            xelPath.AppendChild(xelProjectPath);

            XmlElement xelPtrCheckPath = doc.CreateElement("PtrCheckPath");
            xelPtrCheckPath.InnerText = Convert.ToString(szPtrCheckPath);
            xelPath.AppendChild(xelPtrCheckPath);

            XmlElement xelModulePath = doc.CreateElement("ModulePath");
            xelModulePath.InnerText = Convert.ToString(szModulePath);
            xelPath.AppendChild(xelModulePath);

            XmlElement xelModuleResPath = doc.CreateElement("ModuleResPath");
            xelModuleResPath.InnerText = Convert.ToString(szModuleResPath);
            xelPath.AppendChild(xelModuleResPath);

            XmlElement xelModePath = doc.CreateElement("ModePath");
            xelModePath.InnerText = Convert.ToString(szModePath);
            xelPath.AppendChild(xelModePath);

            XmlElement xelCode = doc.CreateElement("CodeSetting");
            XmlElement xelUserName = doc.CreateElement("UserName");
            xelUserName.InnerText = Convert.ToString(szUserName);
            xelCode.AppendChild(xelUserName);

            XmlElement xelCommentNewCode = doc.CreateElement("CommentNewCode");
            xelCommentNewCode.InnerText = Convert.ToBoolean(bCommentNewCode).ToString();
            xelCode.AppendChild(xelCommentNewCode);

            XmlElement xelPtrCheckSub = doc.CreateElement("PtrCheckSub");
            xelPtrCheckSub.InnerText = Convert.ToBoolean(bPtrCheckSub).ToString();
            xelCode.AppendChild(xelPtrCheckSub);

            root.AppendChild(xelPath);
            root.AppendChild(xelCode);

            doc.Save("Settings.xml");//保存,与exe文件在同一路径
        }
    }
}
